package com.fare.taluo.core.domain.crawl.statistics.impl;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.fare.taluo.base.ddd.repository.EntityHibernateRepository;
import com.fare.taluo.core.domain.crawl.statistics.StatisticsTaskEntity;
import com.fare.taluo.core.domain.crawl.statistics.StatisticsTaskEntityRepository;
import com.fare.taluo.util.StringAndDateAndFileUtil;
@Repository
public class StatisticsTaskEntityRepositoryImpl extends EntityHibernateRepository implements
		StatisticsTaskEntityRepository {

	public List<StatisticsTaskEntity> getStatisticsTaskEntity(String grab_channel,String startTime,String endTime) {
		Session session = null;
		List<StatisticsTaskEntity> lists = new ArrayList<StatisticsTaskEntity>();
		try {
			session = super.getSessionFactory().openSession();
			session.beginTransaction();
			SQLQuery sqlQuery=session.createSQLQuery("{Call STATISTICS_TASK_PRO(?,?,?)}");
			sqlQuery.setString(0, grab_channel);
			sqlQuery.setString(1, startTime);
			sqlQuery.setString(2, endTime);
			lists = this.QueryBy(sqlQuery.list());
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			session.close();
		}
		return lists;
	}

	@Override
	public List<StatisticsTaskEntity> getStatisticsTaskByCondition(
			String grabchannel, String beginTime, String endTime,
			String seachByType, String SymbolType, Long num) {
		String sql = "select * from STATISTICS_TASK where 1=1 ";
		Session session = null;
		List<StatisticsTaskEntity> lists = new ArrayList<StatisticsTaskEntity>();
		session = super.getSessionFactory().openSession();
		if (StringAndDateAndFileUtil.checkString(grabchannel)) {
			sql = sql + " and GRABCHANNEL = '" + grabchannel + "'";
		}

		if (StringAndDateAndFileUtil.checkString(beginTime)
				&& StringAndDateAndFileUtil.checkString(endTime)) {
			sql = sql + " and GRABDATE >=str_to_date('" + beginTime
					+ "','yyyy-MM-dd HH24') and GRABDATE<=str_to_date('" + endTime
					+ "','yyyy-MM-dd HH24')";
		}

		// if(StringUtils.equalsIgnoreCase(seachByType, "All")){
		// if(StringUtils.equalsIgnoreCase(SymbolType, "xyd")){
		// sql = sql +" and total <= "+num+"";
		// }
		// if(StringUtils.equalsIgnoreCase(SymbolType, "dyd")){
		// sql = sql + " and total >= "+num+"";
		// }
		// }
		//
		// if(StringUtils.equalsIgnoreCase(seachByType, "sur")){
		// if(StringUtils.equalsIgnoreCase(SymbolType, "xyd")){
		// sql = sql +" and SUCCESS_NUM <= "+num+"";
		// }
		// if(StringUtils.equalsIgnoreCase(SymbolType, "dyd")){
		// sql = sql + " and SUCCESS_NUM >= "+num+"";
		// }
		// }
		//
		// if(StringUtils.equalsIgnoreCase(seachByType, "far")){
		// if(StringUtils.equalsIgnoreCase(SymbolType, "xyd")){
		// sql = sql + " and FAIL_NUM <= "+num+"";
		// }
		// if(StringUtils.equalsIgnoreCase(SymbolType, "dyd")){
		// sql = sql + " and FAIL_NUM >= "+num+"";
		// }
		// }

		sql += " order by GRABDATE desc,GRABCHANNEL";
		try {
			Query query = session.createSQLQuery(sql);
			lists = this.QueryBy(query.list());
		} catch (Exception e) {
			System.out
					.println("StatisticsTaskEntityRepositoryImpl的getStatisticsTaskByCondition出错");
		} finally {
			session.close();
		}
		return lists;
	}

	private List<StatisticsTaskEntity> QueryBy(List list) {
		List<StatisticsTaskEntity> lists = new ArrayList<StatisticsTaskEntity>();
		for (int i = 0; i < list.size(); i++) {
			StatisticsTaskEntity ste = new StatisticsTaskEntity();
			Object[] row = (Object[]) list.get(i);
			ste.setGrabchannel(row[0].toString());
			
			if(row[1] instanceof Date){
				System.out.println(row[1]);
			}
			ste.setGrabdate(row[1].toString());

			if (row[2] == null) {
				ste.setTotal(0l);
			} else {
				ste.setTotal(Long.parseLong(row[2].toString()));
			}

			if (row[3] == null) {
				ste.setSuccessNum(0l);
			} else {
				ste.setSuccessNum(Long.parseLong(row[3].toString()));
			}

			if (row[4] == null) {
				ste.setFailNum(0l);
			} else {
				ste.setFailNum(Long.parseLong(row[4].toString()));
			}

			if (row[5] == null) {
				ste.setActualNum(0l);
			} else {
				ste.setActualNum(Long.parseLong(row[5].toString()));
			}
			if (row[6] == null) {
				ste.setStatus(-10l);
			} else {
				ste.setStatus(Long.parseLong(row[6].toString()));
			}
			if (row[8] == null) {
				ste.setAllbyteLength(0l);
			} else {
				ste.setAllbyteLength(Long.parseLong(row[8].toString()));
			}

			if (row[9] == null) {
				ste.setWaiting(0l);
			} else {
				ste.setWaiting(Long.parseLong(row[9].toString()));
			}

			ste.setStarting(0l);

			ste.setGrabChannelID(Long.parseLong(row[7].toString()));
			lists.add(ste);
		}
		return lists;
	}

}
